package org.icnmasjid.member.webapp.action;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.icnmasjid.member.Constants;
import org.icnmasjid.member.model.SchoolRegistration;
import org.icnmasjid.member.service.Manager;
import org.icnmasjid.member.service.RegistrationManager;
import org.icnmasjid.member.util.CommonUtil;
import org.icnmasjid.member.util.DesEncrypter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class SchoolRegistrationController extends BaseFormController implements Controller {
    private final Log log = LogFactory.getLog(SchoolRegistrationController.class);
    private Manager manager = null;
    private RegistrationManager registrationManager = null;

	public void setRegistrationManager(RegistrationManager registrationManager) {
		this.registrationManager = registrationManager;
	}

	public void setManager(Manager manager) {
        this.manager = manager;
    }

    public ModelAndView handleRequestInternal(HttpServletRequest request,
                                      HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
        if ( true ) {
        	throw new IllegalStateException("Registration is now closed!");
        }
        SchoolRegistration schoolRegistration = new SchoolRegistration();
        // populate object with request parameters
        BeanUtils.populate(schoolRegistration, request.getParameterMap());
        System.out.println(schoolRegistration.toString());    
        
        SchoolRegistration example = new SchoolRegistration();
        example.setPfname(schoolRegistration.getPfname());
        example.setPlname(schoolRegistration.getPlname());
        example.setPemail(schoolRegistration.getPemail());
        example = registrationManager.getExistingSchoolRegistration(example);
        if ( example != null && example.equals(schoolRegistration) )  {
        	log.error("Duplicate Submission");
        } else {
        	if ( example != null) {
        		schoolRegistration.setId(example.getId());
        		schoolRegistration = registrationManager.merge(schoolRegistration);
        	}
			manager.saveObject(schoolRegistration);        	
	        try {
		        CommonUtil.send(
		        	"registrations@icnschool.com",
		        	schoolRegistration.getPemail(), 
		        	"registrations@icnschool.com",
		        	"ICN Weekend School 2012-2013 Registration",
		        	getText(
							"email.content.registration.success",
							new Object[] {
								schoolRegistration.getPfname(),
								schoolRegistration.getPlname(),
							}
							, request.getLocale()
					)
		        );
	        }catch (Exception e) {
	        	System.out.println(e);
	        	e.printStackTrace();
	        }
        }
        return new ModelAndView("ajax_success");
    }
}